<template>
	<view>
		<view class="wrap">
			<view class="order" v-for="(item,index) in waitOrderData" :key="index">
				<u-skeleton rows="6" :loading="dataLoading">
					<view @click="toDetails(item.goodsId)">
						<view class="top">
							<view class="left">
								<view class="store">{{item.storeName}}</view>
								<u-icon name="arrow-right" color="rgb(203,203,203)" :size="24"></u-icon>
							</view>
							<view class="right">未支付</view>
						</view>
						<view class="item">
							<view class="left">
								<image :src="item.goodsImage" mode="aspectFill"></image>
							</view>
							<view class="content">
								<view class="title u-line-2">{{item.goodsName}}</view>
								<view class="type">已售：{{item.sale}}</view>
								<view class="delivery-time">发货时间 付款后7天内发货</view>
							</view>
							<view class="right">
								<view class="price">
									￥{{toFixedOneDecimal(item.totalPrice)}}
									<text class="decimal">{{toFixedOneDecimal(item.price)}}</text>
								</view>
								<view class="number">x{{item.number}}</view>
							</view>
						</view>
						<view class="total">
							<view style="margin-top: 5px;">
								订单号：{{item.orderId}}
							</view>
							<view>
								共{{item.number}}件商品 合计:
								<text class=" total-price">
									<text class="decimal">￥{{toFixedOneDecimal(item.totalPrice)}}</text>
								</text>
							</view>
						</view>
					</view>
					<view class="right_pay">
						<view @click="handlePay(item.orderId)" class="cart btn u-line-1">支付</view>
					</view>
				</u-skeleton>
			</view>

			<!-- 空状态 -->
			<view v-if="isEmpty">
				<u-empty mode="order" icon="http://cdn.uviewui.com/uview/empty/car.png">
				</u-empty>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		getUserInfo
	} from "@/stores/index.js";
	import {
		queryWaitOrder
	} from "@/api/order/index.js";
	import {
		doHandlePay
	} from "@/api/pay/index.js";

	const user = getUserInfo();
	const isEmpty = ref(false);
	const dataLoading = ref(true);
	onLoad((options) => {
		getWaitOrder();
	})

	const waitOrderData = ref([]);
	//获取当前用户的待付款订单
	const getWaitOrder = () => {
		queryWaitOrder(user.id).then((res) => {
			if (res.code === 200 && res.data !== null) {
				waitOrderData.value = res.data;
				dataLoading.value = false;
			} else {
				isEmpty.value = true;
				dataLoading.value = false;
			}
		})
	}

	//支付方法
	const handlePay = (orderId) => {
		doHandlePay(orderId)
			.then((res) => {
				if (res.code === 200) {
					uni.showToast({
						title: "支付成功！"
					})
					setTimeout(() => {
						uni.redirectTo({
							url: "/pages/my/waitpay/waitpay"
						});
					}, 1000)
				}
			})
	}

	//金额小数点
	function toFixedOneDecimal(num) {
		return parseFloat(num.toFixed(1));
	}

	//跳往商品详情页
	const toDetails = (goodsId) => {
		uni.navigateTo({
			url: "/pages/detail/detail?goodsId=" + goodsId
		})
	}
</script>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;
			display: flex;
			justify-content: space-between;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-info-dark;
			}

			.evaluate {
				color: $u-warning-dark;
				border-color: $u-warning-dark;
			}
		}
	}

	.right_pay {
		display: flex;
		font-size: 28rpx;
		justify-content: flex-end;
		align-items: center;
		margin-top: 15px;

		.btn {
			line-height: 66rpx;
			padding: 0 30rpx;
			border-radius: 36rpx;
			color: #ffffff;
		}

		.cart {
			background-color: #e54d42;
		}

		.cart:active {
			background-color: #fab6b6;
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: auto;
		width: 100%;
		background-color: #F2F6FC;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>